﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication2
{
    public partial class mostraralum : Form
    {
        GestiónAcademia ges = new GestiónAcademia();
        public mostraralum()
        {
            InitializeComponent();
        }

  /*      private void btbuscar_Click(object sender, EventArgs e)
        {
            if (cbFiltro.Text == "Todos")
            {
                grillaAlumnos.DataSource = ges.verAlumnos("");
                grillaAlumnos.DataMember = "ALUMNOS";
                llenarGrilla();
            }
            else if (!(cbFiltro.Text == "Todos"))
            { 
                if (string.IsNullOrWhiteSpace(txtbusqueda.Text) && cbFiltro.Text == "")
                {
                    MessageBox.Show("Debe seleccionar una opción e ingresar un dato para poder buscar.", "Advertencia", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    grillaAlumnos.DataSource = ges.verAlumnos(" WHERE " + cbFiltro.Text + " LIKE '%" + txtbusqueda.Text + "%' OR " + cbFiltro.Text + " LIKE '%" + txtbusqueda.Text + "%' OR " + cbFiltro.Text + " LIKE '%" + txtbusqueda.Text + "%' OR " + cbFiltro.Text + " LIKE '%" + txtbusqueda.Text + "%' OR " + cbFiltro.Text + " LIKE '%" + txtbusqueda.Text + "%' OR " + cbFiltro.Text + " LIKE '%" + txtbusqueda.Text + "%' OR " + cbFiltro.Text + " LIKE '%" + txtbusqueda.Text + "%' OR " + cbFiltro.Text + " LIKE '%" + txtbusqueda.Text + "%' OR " + cbFiltro.Text + " LIKE '%" + txtbusqueda.Text + "%' OR " + cbFiltro.Text + " LIKE '%" + txtbusqueda.Text + "%' OR " + cbFiltro.Text + " LIKE '%" + txtbusqueda.Text + "%' OR " + cbFiltro.Text + " LIKE '%" + txtbusqueda.Text + "%' OR " + cbFiltro.Text + " LIKE '%" + txtbusqueda.Text + "%' OR " + cbFiltro.Text + " LIKE '%" + txtbusqueda.Text + "%' OR " + cbFiltro.Text + " LIKE '%" + txtbusqueda.Text + "%'");
                    grillaAlumnos.DataMember = "ALUMNOS";
                    llenarGrilla();
                }
            }
            else
            {
                MessageBox.Show("Búsqueda sin resultados");
            }
        }*/
    
        private void mostraralum_Load(object sender, EventArgs e)
        {
            cbFiltro.SelectedIndex = 0;
            grillaAlumnos.DataSource = ges.getAlumnos();
            grillaAlumnos.DataMember = "alumnos";
            llenarGrilla();
        }

        public void llenarGrilla()
        {
            grillaAlumnos.Columns[0].HeaderText = "Rut Alumno";
            grillaAlumnos.Columns[1].HeaderText = "Nombre";
            grillaAlumnos.Columns[2].HeaderText = "Fecha Nac";            
            grillaAlumnos.Columns[3].HeaderText = "Sexo";
            grillaAlumnos.Columns[4].HeaderText = "Teléfono";
            grillaAlumnos.Columns[5].HeaderText = "Celular";
            grillaAlumnos.Columns[6].HeaderText = "Email";
            grillaAlumnos.Columns[7].HeaderText = "Avisar a";
            grillaAlumnos.Columns[8].HeaderText = "Dirección";
            grillaAlumnos.Columns[9].HeaderText = "Teléfono";
            grillaAlumnos.Columns[10].HeaderText = "Celular";
            grillaAlumnos.Columns[11].HeaderText = "Email";
            grillaAlumnos.Columns[12].HeaderText = "Grupo Sangre";
            grillaAlumnos.Columns[13].HeaderText = "Alérgico a";
            grillaAlumnos.Columns[14].HeaderText = "Previsión";
            grillaAlumnos.Columns[15].HeaderText = "Estatura";
            grillaAlumnos.Columns[16].HeaderText = "Peso";
            grillaAlumnos.Columns[17].HeaderText = "Calzado";
            grillaAlumnos.Columns[18].HeaderText = "Talla Traje";
            grillaAlumnos.Columns[19].HeaderText = "Talla Buzo";
            grillaAlumnos.Columns[20].HeaderText = "Convenio";
        }


        private void cbFiltro_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbFiltro.Text == "Todos")
            {
                txtbusqueda.Enabled = false;
                grillaAlumnos.DataSource = ges.getAlumnos();
                grillaAlumnos.DataMember = "alumnos";
                llenarGrilla();
            }
            else
            {
                txtbusqueda.Enabled = true;
                txtbusqueda.Text = "";
                if (cbFiltro.Text == "rut")
                {
                    txtbusqueda.MaxLength = 8;
                }
                if (cbFiltro.Text == "sexo")
                {
                    txtbusqueda.MaxLength = 9;
                }
                if (cbFiltro.Text == "nombre")
                {
                    txtbusqueda.MaxLength = 50;
                }
                if (cbFiltro.Text == "telefono" || cbFiltro.Text == "celular")
                { txtbusqueda.MaxLength = 11; }
                if (cbFiltro.Text == "estatura" || cbFiltro.Text == "peso")
                { txtbusqueda.MaxLength = 3; }
                if (cbFiltro.Text == "talla_traje" || cbFiltro.Text == "talla_buzo")
                { txtbusqueda.MaxLength = 3; }
                if (cbFiltro.Text == "num_calzado" || cbFiltro.Text == "idconvenio")
                { txtbusqueda.MaxLength = 2; }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Form.ActiveForm.Visible = false;
            estudiante es = new estudiante();
            es.ShowDialog();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            Form.ActiveForm.Visible = false;
            modificaralum m = new modificaralum();
            m.ShowDialog();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Form.ActiveForm.Visible = false;
            bloqalum b = new bloqalum();
            b.ShowDialog();
        }

        private void txtbusqueda_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (cbFiltro.Text == "telefono" || cbFiltro.Text == "celular" || cbFiltro.Text == "estatura" || cbFiltro.Text == "peso" || cbFiltro.Text == "num_calzado" || cbFiltro.Text == "idconvenio")
            {
                if (Char.IsDigit(e.KeyChar))
                {
                    e.Handled = false;
                }
                else
                    if (Char.IsControl(e.KeyChar)) //permitir teclas de control como retroceso 
                    {
                        e.Handled = false;
                    }
                    else
                    {
                        //el resto de teclas pulsadas se desactivan 
                        e.Handled = true;
                    }
            }
        }

        private void txtbusqueda_TextChanged(object sender, EventArgs e)
        {
            grillaAlumnos.DataSource = ges.verAlumnos(" WHERE " + cbFiltro.Text + " LIKE '%" + txtbusqueda.Text + "%' OR " + cbFiltro.Text + " LIKE '%" + txtbusqueda.Text + "%' OR " + cbFiltro.Text + " LIKE '%" + txtbusqueda.Text + "%' OR " + cbFiltro.Text + " LIKE '%" + txtbusqueda.Text + "%' OR " + cbFiltro.Text + " LIKE '%" + txtbusqueda.Text + "%' OR " + cbFiltro.Text + " LIKE '%" + txtbusqueda.Text + "%' OR " + cbFiltro.Text + " LIKE '%" + txtbusqueda.Text + "%' OR " + cbFiltro.Text + " LIKE '%" + txtbusqueda.Text + "%' OR " + cbFiltro.Text + " LIKE '%" + txtbusqueda.Text + "%' OR " + cbFiltro.Text + " LIKE '%" + txtbusqueda.Text + "%' OR " + cbFiltro.Text + " LIKE '%" + txtbusqueda.Text + "%' OR " + cbFiltro.Text + " LIKE '%" + txtbusqueda.Text + "%' OR " + cbFiltro.Text + " LIKE '%" + txtbusqueda.Text + "%' OR " + cbFiltro.Text + " LIKE '%" + txtbusqueda.Text + "%' OR " + cbFiltro.Text + " LIKE '%" + txtbusqueda.Text + "%'");
            grillaAlumnos.DataMember = "ALUMNOS";
            llenarGrilla();
        }
    }
}
